<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:tenantFunctionParametersVersion:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:tenantFunctionParametersVersion:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:tenantFunctionParametersVersion:remove']"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="mainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="类名" align="center" prop="className" />
      <el-table-column label="函数编码" align="center" prop="functionCode" />
      <el-table-column label="参数编码" align="center" prop="parameterCode" />
      <el-table-column label="参数名" align="center" prop="parameterName" />
      <el-table-column label="参数类型" align="center" prop="parameterType" />
      <el-table-column label="排序号" align="center" prop="sortNumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:tenantFunctionParametersVersion:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:tenantFunctionParametersVersion:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 添加或修改租户功能点参数版本表对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="mainRef" :model="form"  label-width="auto">
        <el-form-item label="类名" prop="className">
          <el-input v-model="form.className" disabled />
        </el-form-item>
        <el-form-item label="函数编码" prop="functionCode">
          <el-input v-model="form.functionCode" disabled />
        </el-form-item>
        <el-form-item label="参数编码" prop="parameterCode">
          <el-input v-model="form.parameterCode" disabled />
        </el-form-item>
        <el-form-item label="参数名" prop="parameterName">
          <el-input v-model="form.parameterName" placeholder="请输入参数名" />
        </el-form-item>
        <el-form-item label="参数类型" prop="parameterType">
          <el-input v-model="form.parameterType" placeholder="请输入参数类型" />
        </el-form-item>
        <el-form-item label="排序号" prop="sortNumber">
          <el-input v-model="form.sortNumber" placeholder="请输入排序号" />
        </el-form-item>
        <el-form-item label="参数描述" prop="parameterDesc">
          <el-input v-model="form.parameterDesc" placeholder="请输入参数描述" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">

import {getCurrentInstance, ref, watch,reactive} from "vue";
import {getRandomLong} from "@/utils/ruoyi";

const { proxy } = getCurrentInstance();

const perms = {
  edit: 'business:tenantFunctionParametersVersion:edit',
  remove: 'business:tenantFunctionParametersVersion:remove'
};

const mainList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const form= ref({
  parameterId:null,
  enterpriseCode:null,
  className:null,
  functionCode:null,
  parameterCode:null,
  parameterName:null,
  parameterType:null,
  sortNumber:null,
  createBy:null,
  createTime:null,
  updateBy:null,
  updateTime:null,
  parameterDesc:null,
  version:null,
  isCurrent:null
})
const props = defineProps(['modelValue'])
const emit = defineEmits(['update:modelValue'])


// 表单重置
function reset() {
  form.value = {
    parameterId:null,
    enterpriseCode:null,
    className:null,
    functionCode:null,
    parameterCode:null,
    parameterName:null,
    parameterType:null,
    sortNumber:null,
    createBy:null,
    createTime:null,
    updateBy:null,
    updateTime:null,
    parameterDesc:null,
    version:null,
    isCurrent:null
  };
  proxy.resetForm("mainRef");
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");

}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.parameterId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加租户功能点参数版本表";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _parameterid = row.parameterId || ids.value
  //循环mainList查找parameterid等于row.parameterid的行
  for(let i=0;i<mainList.value.length;i++){
    if(mainList.value[i].parameterId==_parameterid){
      form.value = JSON.parse(JSON.stringify(mainList.value[i]));
      open.value = true;
      break;
    }
  }

}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["mainRef"].validate(valid => {
    //debugger;
    if (valid) {
      if (form.value.parameterId != null) {
        for (let i = 0; i < mainList.value.length; i++) {
          if (mainList.value[i].parameterId == form.value.parameterId) {
            // mainList.value[i] = form.value;
            mainList.value[i]=JSON.parse(JSON.stringify(form.value));

            open.value = false;
            // reset();
            break;
          }
        }
      }else{
        form.value.parameterId = getRandomLong();
        mainList.value.push(JSON.parse(JSON.stringify(form.value)));
        console.log(mainList.value);
        open.value = false;
        reset();
      }
      emit('update:modelValue', mainList.value)
    }
  });
}
function cancel() {
  open.value = false;
  reset();
}
/** 删除按钮操作 */
function handleDelete(row) {
    const _parameterids = Array.isArray(row.parameterId)
      ? row.parameterId
      : [row.parameterId || ids.value].flat();
    proxy.$modal.confirm('是否确认删除租户功能点参数版本表编号为"' + _parameterids + '"的数据项？').then(function() {
    //删除指定行
    for(let i=mainList.value.length-1;i>=0;i--){
      for(let j=0;j<_parameterids.length;j++)
      if(mainList.value[i].parameterId == _parameterids[j]){
        mainList.value.splice(i,1);
      }
    }
    emit('update:modelValue', mainList.value)
  }).then(() => {
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}
watch(props,(newValue, oldValue) => {
     mainList.value = props.modelValue;
    }
)
</script>
